/*
 * Copyright 2014 Semidata. All rights reserved.
 */
package com.semidata.trp.report.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.SqlRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.semidata.trp.report.mapper.ReportMapper;
import com.semidata.trp.report.model.MonitorServerModel;
import com.semidata.trp.report.model.ResourceMapModel;
import com.semidata.trp.report.model.StrategyMapModel;

/**
 * @author shewei@semidata.com, She Wei
 * 
 */
public class SqlUtils {

	private static SqlSessionFactory sessionFactory = null;
	static {
		String resource = "conf/mybatis-report.xml";
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		sessionFactory = new SqlSessionFactoryBuilder()
		        .build(new InputStreamReader(inputStream));
	}

	// 为了灵活获取session
	public static SqlSessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static SqlSession getSession() {
		return sessionFactory.openSession();
	}

	public static void close(SqlSession session) {

		if (session != null) {
			session.close();
		}
	}

	public static void executeSql(String... sqls) {
		SqlSession session = getSession();
		SqlRunner runner = new SqlRunner(session.getConnection());
		try {
			runner.run("start transaction");
			for (String sql : sqls)
				runner.run(sql);
			runner.run("commit");
		} catch (SQLException e) {
			try {
				runner.run("rollback");
			} catch (SQLException e1) {
			}
			throw new RuntimeException(e);
		} finally {
			session.close();
		}
	}

	public static List<String> getMonitorServerNames() {
		ArrayList<String> list = new ArrayList<>();
		
		SqlSession session = getSession();
		ReportMapper mapper = session.getMapper(ReportMapper.class);
		
		List<MonitorServerModel> models = mapper.getMonitorServers();
		
		session.commit();
		close(session);
		
		for (MonitorServerModel model : models) {
			list.add(model.getName());
		}
		
		return list;
	}

	public static Map<String, Map<Integer, Integer>> getBusinessMap() {
		
		Map<String, Map<Integer, Integer>> map = new HashMap<>();
		HashMap<Integer, Integer> mediaMap = new HashMap<Integer, Integer>();
		HashMap<Integer, Integer> positionMap = new HashMap<Integer, Integer>();
		HashMap<Integer, Integer> typeMap = new HashMap<Integer, Integer>();
		HashMap<Integer, Integer> orgMap = new HashMap<Integer, Integer>();
		
		
		map.put("media", mediaMap);
		map.put("position", positionMap);
		map.put("type", typeMap);
		map.put("org", orgMap);
		
		SqlSession session = getSession();
		ReportMapper mapper = session.getMapper(ReportMapper.class);
		
		List<ResourceMapModel> resourceMap = mapper.getResourceMap();
		List<StrategyMapModel> strategyMap = mapper.getStrategyMap();
		session.commit();
		close(session);
		
		for (ResourceMapModel model : resourceMap) {
			
			mediaMap.put(model.getId(), model.getMediaId());
			positionMap.put(model.getId(), model.getPositionId());
			typeMap.put(model.getId(), model.getType());
		}
		
		for (StrategyMapModel model : strategyMap) {
			orgMap.put(model.getId(), model.getOrgId());
		}
		return map;
	}
	
	public static void main(String[] args) {
		//System.out.println(getBusinessMap());
		System.out.println(getMonitorServerNames());
	}

}
